/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.tools;

import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;

/**
 * Utility class for translating raw mesages to filled up messages with dynamic
 * parameters.
 * <p>
 * Raw messages are loaded from properties files during initialization process.
 * 
 * @author Jan Pastor 
 */
public class Messages {

    private Messages() {
    }

    // ----------------------------------------------------------------------

    /**
     * Initialize this singleton class.
     * <p>
     * Can be reinitialized using this method more than once.
     * <p>
     * This implementation use <code>sk.ana.framework.tools.Properties</code>
     * utility singleton, so you don't need to call this method if you already
     * initialize <code>Properties</code> class.
     * 
     * @param msgSrcPaths base name of properties files (package + name),
     *            without '.properties' extension.
     * @param locale locale used for loads language-specific properties files.
     *            Use null to use JVM default locale.
     */
    public static void init(List<String> msgSrcPaths, Locale locale) {
        Properties.init(msgSrcPaths, locale);
    }

    /**
     * Translate message code (key) to message text.
     * <p>
     * See documentation for class <code>java.text.MessageFormat</code> for
     * details about message patterns.
     * 
     * @param code message code (key), searched in property files.
     * @param params array of parameters used to fill up message pattern.
     * @return filled up message text or given code if no message pattern found
     *         for specified code.
     */
    public static String getMessage(String code, Object... params) {
        String msgPattern = Properties.getProperty(code);
        if (msgPattern == null) { // code not found in properties files
            return code;
        }
        MessageFormat mf = new MessageFormat(msgPattern, Properties.getLocale());
        return mf.format(params, new StringBuffer(), null).toString();
    }

    /**
     * Translate message code (key) to message text.
     * <p>
     * See documentation for class <code>java.text.MessageFormat</code> for
     * details about message patterns.
     * @param code message code (key), searched in property files.
     * @param params parameters used to fill up message pattern. If null,
     *            returns only localized message.
     * @return filled up message text or null if no message pattern found for
     *         specified code.
     */
    public static String getMessageOrNull(String code, Object... params) {
        String msgPattern = Properties.getProperty(code);
        if (msgPattern == null) {
            // message translation missing in catalog
            return null;
        }
        if (params == null) {
            // no params, return translated message
            return msgPattern;
        }
        MessageFormat mf = new MessageFormat(msgPattern, Properties.getLocale());
        return mf.format(params, new StringBuffer(), null).toString();
    }

    /**
     * Translate message code (key) to message text.
     * 
     * @param code message code (key), searched in property files.
     * @return message text or given code if no message pattern found for
     *         specified code.
     */
    public static String getMessage(String code) {
        String msg = Properties.getProperty(code);
        if (msg == null) { // code not found in properties files
            return code;
        }
        return msg;
    }

}
